package com.rc.evcharger.mq;

import com.rc.evcharger.model.elec.ElecPile;
import com.rc.evcharger.protocol.ylevcharger.service.YlChargeMessageService;
import com.rc.evcharger.service.elec.ElecPileService;
import com.rc.evcharger.action.StdBizAction;
import com.rc.evcharger.dto.msg.TerminalMessage;
import com.rc.evcharger.service.ChargeMessageService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 异步方法处理类（按SpringAsyncConfig配置）
 * Created by Sven on 2021/06/28
 */
@Component
public class AsyncTask {
    private static Logger logger = LogManager.getLogger(AsyncTask.class);

    @Autowired
    private ChargeMessageService chargeMessageService;

    @Autowired
    private StdBizAction stdBizAction;

    @Autowired
    private ElecPileService elecPileService;


    /**
     * 异步方法,@Async使用Bean Name为taskExecutor的线程池
     * @param msg
     */
    @Async("taskExecutor1")
    public void hearBeat(TerminalMessage msg) {
        ElecPile pile = elecPileService.selectByPileNo(msg.getTerminalId());
        if (pile == null) {
            logger.info("心跳指令找不到对应桩编号:{}", msg.getRtua());
            return;
        }

        chargeMessageService.hearBeat(pile, msg);

        //logger.info("异步执行心跳,:{},:{}", pile.getPileNo(), pile.getStatus());
    }

    /**
     * 异步方法,@Async使用Bean Name为taskExecutor的线程池
     * @param msg
     */
    @Async("taskA5Executor1")
    public void chargeReport(TerminalMessage msg) {
        stdBizAction.chargeReport(msg);
        logger.info("异步执行A5指令:pilertua:{}", msg.getRtua());
    }

    /**
     * 异步方法,@Async使用Bean Name为taskExecutor的线程池
     * @param msg
     */
    @Async("taskA5Executor2")
    public void chargeReport2(TerminalMessage msg) {
        stdBizAction.chargeReport(msg);
        logger.info("异步执行A5指令:pilertua:{}", msg.getRtua());
    }

    /**
     * 异步方法,@Async使用Bean Name为taskExecutor的线程池
     * @param msg
     */
    @Async("taskA5Executor3")
    public void chargeReport3(TerminalMessage msg) {
        stdBizAction.chargeReport(msg);
        logger.info("异步执行A5指令:pilertua:{}", msg.getRtua());
    }

    /**
     * 异步方法,@Async使用Bean Name为taskExecutor的线程池
     * @param msg
     */
    @Async("taskA5Executor4")
    public void chargeReport4(TerminalMessage msg) {
        stdBizAction.chargeReport(msg);
        logger.info("异步执行A5指令:pilertua:{}", msg.getRtua());
    }

    /**
     * 异步方法,@Async使用Bean Name为taskExecutor的线程池
     * @param msg
     */
    @Async("taskExecutor5")
    public void chargeReport5(TerminalMessage msg) {
        stdBizAction.chargeReport(msg);
        logger.info("异步执行A5指令:pilertua:{}", msg.getRtua());
    }
}
